package com.fenghongzhang.day009;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Pair;
import android.view.View;
import android.widget.Toast;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    private SQLiteDatabase db;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        MySqlHelp mySqlHelp = new MySqlHelp(this, "student.db", null, 4);
        db = mySqlHelp.getReadableDatabase();//创建一个名称为"student.db
    }

    public void create(View view) {
//        MySqlHelp mySqlHelp = new MySqlHelp(this, "student.db", null, 3);
//         db = mySqlHelp.getReadableDatabase();//创建一个名称为"student.db
        //库 -> 表 -> 数据
    }

    public void createTable(View view) {
        //创建表
        db.execSQL("create table a1811 (id integer primary key autoincrement, name varchar(20), age integer)");

    }

    public void insert(View view) {
//        ContentValues contentValues = new ContentValues();
//        //第一个是字段名, 第二个是给字段赋值
//        contentValues.put("name","小明");
//        contentValues.put("age",16);
//        //1,表名,2 null字段怎么办,还是null,具体的值.
//        db.insert("a1811",null,contentValues);

        //原生插入
//        String sql = "insert into a1811 (id,name,age) values(null,'大大明',18)";

        //开启事务
        db.beginTransaction();


        String sql = "insert into a1811 values(null,?,?)";
        //1, sql语句, 2,是给?(占位符)赋值
        db.execSQL(sql,new Object[]{"大大明",18});


        //设置事务提交
        db.setTransactionSuccessful();
        //结束事务.
        db.endTransaction();


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        db.close();
    }

    public void query(View view) {

        //原生
        String sql = "select * from a1811";
        Cursor cursor = db.rawQuery(sql, null);
        if(cursor != null){
            while (cursor.moveToNext()){
                String name = cursor.getString(cursor.getColumnIndex("name"));
                String age = cursor.getString(cursor.getColumnIndex("age"));
                Toast.makeText(this, name+age, Toast.LENGTH_SHORT).show();
            }
            cursor.close();
        }


        //select * from a1811111 ;
//        Cursor cursor = db.query("a1811", null, null, null, null, null, null);
//        if(cursor != null){
//            //向下移动一行,如果有数据,返回true,如果没有数据返回false;
//            while(cursor.moveToNext()){
//                //1,2
//                String name = cursor.getString(cursor.getColumnIndex("name"));
//                int age = cursor.getInt(cursor.getColumnIndex("age"));
//                Toast.makeText(this, name+age, Toast.LENGTH_SHORT).show();
//            }
//            //3关闭游标
//            cursor.close();
//        }

    }

    //更新
    public void update(View view) {

        String sql = "update a1811 set name = ? , age = ? where id = ?";
        db.execSQL(sql,new Object[]{"dadadaming",180,3});


         // update a1811 set name = "大明" where id = 1;
//        ContentValues contentValues = new ContentValues();
//        contentValues.put("name","大明");
//        db.update("a1811",contentValues,"id = ?",new String[]{"1"});
    }

    //删除
    public void del(View view) {
        String sql = "delete from a1811";
        db.execSQL(sql,new Object[]{});

//        db.delete("a1811","id = ?",new String[]{"2"});
    }
}
